/*
 *  Copyright 2010 Andrew Kroh
 *
 *  Kroh-IO is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Kroh-IO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "kroh_serial_port.h"

#include "serial_port.h"
#include "util.h"

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    setDebugLevel
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_kroh_io_KrohSerialPort_nativeSetDebugLevel
  (JNIEnv *env, jclass kroh_serial_port_class, jint debug_level)
{
    // This method is called once during initialization
    // so print the copyright notice from here:
    print_copyright();

    switch (debug_level)
    {
        case kroh_io_KrohSerialPort_DEBUG_OFF:
            set_debug_level(DEBUG_OFF);
            break;
        case kroh_io_KrohSerialPort_DEBUG_ERROR:
            set_debug_level(DEBUG_ERROR);
            break;
        case kroh_io_KrohSerialPort_DEBUG_WARN:
            set_debug_level(DEBUG_WARN);
            break;
        case kroh_io_KrohSerialPort_DEBUG_INFO:
            set_debug_level(DEBUG_INFO);
            break;
        case kroh_io_KrohSerialPort_DEBUG_DEBUG:
            set_debug_level(DEBUG_DEBUG);
            break;
        case kroh_io_KrohSerialPort_DEBUG_TRACE:
            set_debug_level(DEBUG_TRACE);
            break;
        default:
            printf("set_debug_level() received unhandled "
                   "debug_level of <%d>.", (int)debug_level);
            set_debug_level(DEBUG_OFF);
    }

    dp(DEBUG_TRACE, "set_debug_level(%d)\n", (int)debug_level);
}
  
/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    testSerialPorts
 * Signature: (Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeTestSerialPort
  (JNIEnv *env, jclass kroh_serial_port_class, jstring port_name)
{
    const char *name = (*env)->GetStringUTFChars(env, port_name, 0);

    int success = test_serial_port(name);

    (*env)->ReleaseStringUTFChars(env, port_name, name);
    
    return success ? JNI_TRUE : JNI_FALSE;
}
  
/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeOpen
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeOpen
  (JNIEnv *env, jobject kroh_serial_port, jstring port_name)
{
    const char *name = (*env)->GetStringUTFChars(env, port_name, 0);

    int fileDescriptor = open_port(name);

    (*env)->ReleaseStringUTFChars(env, port_name, name);
    
    return fileDescriptor;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeGetBaudRate
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeGetBaudRate
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return get_baud_rate((int)fd);
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeGetDataBits
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeGetDataBits
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return get_data_bits((int)fd);
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeGetStopBits
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeGetStopBits
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return get_stop_bits((int)fd);
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeGetParity
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeGetParity
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return get_parity((int)fd);
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeSendBreak
 * Signature: (II)V
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeSendBreak
  (JNIEnv *env, jobject kroh_serial_port, jint fd, jint duration)
{
    return send_break((int)fd, (int)duration) == 0 ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeSetSerialPortParams
 * Signature: (IIIIII)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeSetSerialPortParams
  (JNIEnv *env, jobject kroh_serial_port, jint fd, jint baud_rate, 
   jint databits, jint stopbits, jint parity, jint flow_control)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeGetFlowControlMode
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeGetFlowControlMode
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return 0;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeSetDTR
 * Signature: (IZ)V
 */
JNIEXPORT void JNICALL Java_kroh_io_KrohSerialPort_nativeSetDTR
  (JNIEnv *env, jobject kroh_serial_port, jint fd, jboolean state)
{
    return;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeIsDTR
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeIsDTR
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeSetRTS
 * Signature: (IZ)V
 */
JNIEXPORT void JNICALL Java_kroh_io_KrohSerialPort_nativeSetRTS
  (JNIEnv *env, jobject kroh_serial_port, jint fd, jboolean state)
{
    return;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeIsRTS
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeIsRTS
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeIsCTS
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeIsCTS
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeIsDSR
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeIsDSR
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeIsRI
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeIsRI
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeIsCD
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeIsCD
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return JNI_TRUE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeClose
 * Signature: (I)V
 */
JNIEXPORT jboolean JNICALL Java_kroh_io_KrohSerialPort_nativeClose
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return close_port((int)fd) ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeAvailable
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeAvailable
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return 0;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeRead
 * Signature: (I[BIIII)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeRead
  (JNIEnv *env, jobject kroh_serial_port, jint fd, jbyteArray dest_array, 
   jint offset, jint num_bytes_to_read, jint receive_timeout_ms, 
   jint num_receive_threshold_bytes)
{
    return 0;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeWrite
 * Signature: (I[BII)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeWrite
  (JNIEnv *env, jobject kroh_serial_port, jint fd, jbyteArray src_array, 
   jint offset, jint num_bytes_to_write)
{
    return 0;
}

/*
 * Class:     kroh_io_KrohSerialPort
 * Method:    nativeFlush
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_kroh_io_KrohSerialPort_nativeFlush
  (JNIEnv *env, jobject kroh_serial_port, jint fd)
{
    return 0;
}

